<?php

/**
 * Created by Sublime.
 * User: tchenym
 * Date: 2018/08/21
 * Time: 上午午10:25
 */
class Store_model extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
        $this->load->model('customer_model');
    }

    //根据shop_id获取店铺信息
    public function getStoreInfo($shop_id)
    {
        $where              = array('id' => $shop_id);
        $result             = $this->db->where($where)->from('app_shop')->get()->row_array();
        $result['shop_pic'] = $this->db->where(array('k' => 'pic'))->from('app_key_value')->get()->row_array()['v'];
        return $result;
    }

    //根据店铺shop_id得到店铺所有的商品信息及库存
    public function getAllGoodsInfo($shop_id, $session_key)
    {
        $where = array('a.shop_id' => $shop_id);
        $list  = $this->db->select('a.num as goods_num,b.*')->where($where)->from('app_shop_goods a')
            ->join('app_goods b', 'b.id=a.goods_id and b.status=1')
            ->get()->result_array();
        $user_id = $this->customer_model->sessionKeyToUserId($session_key);
        if ($list) {
            foreach ($list as $key => $value) {
                $where = array('user_id' => $user_id, 'shop_id' => $shop_id, 'goods_id' => $value['id'], 'type' => 2);
                $cart  = $this->db->where($where)->from('app_cart')->get()->row_array();
                if ($cart) {
                    $list[$key]['cartNum'] = $cart['num'];
                } else {
                    $list[$key]['cartNum'] = 0;
                }
            }
            return $list;
        } else {
            return false;
        }

    }

    //根据商品id获取该商品的所有库存
    public function getAllGoodsNumByGoodsId($goods_id)
    {
        $where = array('id' => $goods_id, 'status' => 1);
        $num   = $this->db->where($where)->from('app_goods')->get()->row_array()['num'];
        return $num;
    }

    //根据店铺id和商铺id获取该店铺的所有库存
    public function getShopGoodsAllNum($goods_id, $shop_id)
    {
        $where = array('goods_id' => $goods_id, 'shop_id' => $shop_id);
        $num   = $this->db->where($where)->from('app_shop_goods')->get()->row_array()['num'];
        return $num;
    }

    //根据商品id获取商品详情
    public function getGoodsDetail($goods_id)
    {
        $where   = array('goods_id' => $goods_id);
        $list[0] = $this->db->where($where)->from('app_goods_detail')->order_by('id', 'asc')->get()->result_array();
        $where   = array('id' => $goods_id, 'status' => 1);
        $list[1] = $this->db->where($where)->from('app_goods')->get()->row_array();
        if (!$list[1]) {
            send_json(array('code' => 4004, 'data' => '', 'msg' => "id为" . $goods_id . "的商品不存在或者已下架"));
        }
        return $list;
    }

    //定时扫描库存
    public function ScanInventory()
    {
        $where = array('num<' => 4);
        $list  = $this->db->where($where)->from('app_shop_goods')->get()->result_array();
        if ($list) {
            foreach ($list as $key => $value) {
                $where = array('shop_id' => $value['shop_id'], 'goods_id' => $value['goods_id']);
                $row   = $this->db->where($where)->from('app_message')->get()->row_array();
                if (!$row) {
                    $dataArr['shop_id']    = $value['shop_id'];
                    $dataArr['goods_id']   = $value['goods_id'];
                    $dataArr['num']        = $value['num'];
                    $dataArr['created_at'] = date("Y-m-d H:i:s", time());
                    $this->db->insert('app_message', $dataArr);
                }
            }
        }
    }

    //添加补货单
    // public function addStock($param)
    // {
    //     $dataArr['shop_id']    = $param['shop_id'];
    //     $dataArr['goods_id']   = $param['goods_id'];
    //     $dataArr['num']        = $param['num'];
    //     $dataArr['created_at'] = date("Y-m-d H:i:s", time());
    //     $this->db->insert('app_restock', $dataArr);
    // }
}
